// import { feedbackToast } from '@/utils/common';
import { IMSDK, IMMethods, getCleanText } from '@/utils/im';
import type { MessageItem } from '@openim/client-sdk/lib/types/entity';
import type { Ref } from 'vue';

type CreateNomalMessageProps = {
  messageContent: Ref<string>;
};

export default function useCreateNomalMessage({ messageContent }: CreateNomalMessageProps) {
  const getCleanTextWithBr = () => {
    let text = messageContent.value;
    text = text.replace(/<div>/g, '\n').replace(/<\/div>/g, '');
    return getCleanText(text);
  };

  const getTextMessage = async () => {
    const formattedText = getCleanTextWithBr();
    console.log(formattedText);

    return ((await IMSDK.asyncApi(IMMethods.CreateTextMessage, IMSDK.uuid(), formattedText)) as any)
      .data;
  };

  const switchNomalMessage = async () => {
    const message = (await getTextMessage()) as MessageItem;
    if (!message) {
      // feedbackToast({
      //   error: 'create message failed',
      //   message: 'create message failed',
      // });
      return;
    }
    return message;
  };

  return {
    switchNomalMessage,
    getCleanText,
  };
}
